package com.xpqh.ksg.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;


/**
 * 参数输出拦截器
 * @author 36115
 * 2019年7月19日 上午9:03:41
 */
@Component
public class ReqInterceptor implements HandlerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(ReqInterceptor.class);

	@Override
	public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3)
			throws Exception {
		//Logger.info(res.toString());
	}

	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3)
			throws Exception {
	}

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object arg2) throws Exception {
		Enumeration<String> e = req.getParameterNames();
		StringBuffer sb = new StringBuffer();
		sb.append("PARAM: ");
		while(e.hasMoreElements()){
			String key = e.nextElement();
			sb.append(key);
			sb.append("=" + req.getParameter(key) + "&");
		}
		sb.deleteCharAt(sb.length()-1);
		System.out.println("调用方法和参数----"+req.getMethod() + ": " + req.getRequestURI().toString() + "\n" + sb.toString());
		logger.info("调用方法和参数----"+req.getMethod() + ": " + req.getRequestURI().toString() + "\n" + sb.toString());
		return true;
	}

}
